package dilmaj.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import dilmaj.shared.*;
import java.util.*;

public class StarPanel extends PopupPanel {
	//private StarServiceAsync starSvc = GWT.create(StarService.class);
	private DilmajConstants constants = GWT.create(DilmajConstants.class);

	StarEntryVO dataVO;
	VerticalPanel mainPanel=new VerticalPanel();
	HorizontalPanel privilegePanel=new HorizontalPanel();
	
	TextBox captionBox=new TextBox();
	TextBox suggestionBox=new TextBox();
	Button closeButton=new Button(constants.close());
	TextArea descriptionBox=new TextArea();
	Label rankLabel=new Label();
	List<Button> privilegeButtons=new ArrayList<Button>();
	Button removeButton=new Button(constants.remove());
	
	StarSearchTriggerButton areasButton;
	StarSearchTriggerButton sourcesButton;
	StarSearchTriggerButton languagesButton;
	StarSearchTriggerButton commentsButton;
	StarSearchTriggerButton usecasesButton;
	
	/*HorizontalPanel commentsPanel=new HorizontalPanel();
	HorizontalPanel usecasesPanel=new HorizontalPanel();*/
	
	List<StarTermVO> terms=new ArrayList<StarTermVO>();
	List<StarSuggestionVO> suggestions=new ArrayList<StarSuggestionVO>();
	StarSuggestionVO suggestionVO=null;
	List<StarEntryVO> attachments=new ArrayList<StarEntryVO>();
	
	FlexTable mainTable=new FlexTable();
	String type;
	
	private StarButtonPanel buttonPanel;
	
	public StarPanel(StarEntryVO dataVO, StarButtonPanel sourceButton) {
		buttonPanel=sourceButton;
		this.dataVO=dataVO;
		type=dataVO.getTitle();
		this.setTitle(dataVO.getCaption()+":"+dataVO.getTitle());
	
		closeButton.addClickHandler(new CloseHandler(this));
		captionBox.setText(dataVO.getCaption());
		descriptionBox.setText(dataVO.getDescription());
		
		if (dataVO.getUsername()!=null)
			mainTable.setText(0, 0, dataVO.getUsername()+":");
		mainTable.setWidget(0, 1, captionBox);
		if (type.compareToIgnoreCase("term")==0) {
			//mainTable.setText(0, 2, "=");
			mainTable.setWidget(0, 2, suggestionBox);
		}
		HorizontalPanel rankPanel=new HorizontalPanel();
		VerticalPanel rankerPanel=new VerticalPanel();
		rankerPanel.add(new RankButton(this, 1));
		rankerPanel.add(new RankButton(this, -1));
		rankPanel.add(rankerPanel);
		rankPanel.add(rankLabel);
		mainTable.setWidget(0, 3, descriptionBox);
		mainTable.setWidget(0, 4, rankPanel);
		mainTable.setWidget(0, 5, rankLabel);
		mainTable.setWidget(0,6,closeButton);
		setRank(dataVO.getRank());
		
		areasButton=new StarSearchTriggerButton(constants.area(), constants.areas(), dataVO, this);
		sourcesButton=new StarSearchTriggerButton(constants.source(), constants.sources(), dataVO, this);
		commentsButton=new StarSearchTriggerButton(constants.comment(), constants.comments(), dataVO, this);
		languagesButton=new StarSearchTriggerButton(constants.language(), constants.languages(), dataVO, this);
		usecasesButton=new StarSearchTriggerButton(constants.usecase(), constants.usecase(), dataVO, this);
		
		refresh();
		
		removeButton.addClickHandler(new RemoveHandler(this));
		Iterator pIterator=dataVO.getPrivileges().iterator();
		while (pIterator.hasNext()) {
			StarPrivilegeVO currentPrivilege=(StarPrivilegeVO)pIterator.next();
			addPrivilege(currentPrivilege.getUsername());
		}
		mainPanel.add(mainTable);
		mainPanel.add(privilegePanel);
		
		add(mainPanel);
		refreshLogin();
	}

	public void refreshLogin() {
		if (DataStore.getUserid()==-1) {
			captionBox.setReadOnly(true);
			suggestionBox.setReadOnly(true);
			removeButton.setVisible(false);
		} else {
			if (dataVO.getUsername()!=null) {
				if (DataStore.getUsername().compareTo(dataVO.getUsername())==0) {
					captionBox.setReadOnly(false);
					suggestionBox.setReadOnly(false);
					removeButton.setVisible(true);
				} else {
					captionBox.setReadOnly(true);
					suggestionBox.setReadOnly(true);
					removeButton.setVisible(false);
				}
			} else {
				captionBox.setReadOnly(true);
				suggestionBox.setReadOnly(true);
				removeButton.setVisible(false);
			}
		}
	}
	
	public void addPrivilege(String username) {
		Button privilegeButton=new Button("*");
		privilegeButtons.add(privilegeButton);
		privilegeButton.setTitle(username);
		privilegeButton.setEnabled(false);
		privilegePanel.add(privilegeButton);
	}
	
	public void setRank(long rank) {
		rankLabel.setText(rank+"");
	}
	
	public void refresh() {
		int n=mainTable.getRowCount();
		for (int i=1;i<n;i++)
			mainTable.removeRow(1);
		
		if (type.compareToIgnoreCase("term")==0) {
			mainTable.setWidget(1, 0, areasButton);
			mainTable.setWidget(2, 0, sourcesButton);
			mainTable.setWidget(3, 0, languagesButton);
			mainTable.setWidget(4, 0, commentsButton);
			privilegePanel.add(removeButton);
		}
		mainTable.setWidget(5, 0, usecasesButton);
		
		attachments=dataVO.getAttachments();
		Iterator iterator=attachments.iterator();
		
		int areaCounter=1;
		int sourceCounter=1;
		int languageCounter=1;
		int commentCounter=1;
		int usecaseCounter=1;
		
		while (iterator.hasNext()) {
			StarEntryVO currentEntry=(StarEntryVO)iterator.next();
			try {
				suggestionVO=(StarSuggestionVO)currentEntry;
				if (type.compareToIgnoreCase("term")==0)
					suggestionBox.setText(suggestionVO.getCaption());
			} catch (ClassCastException cce0) {
				try {
					StarAreaVO areaVO=(StarAreaVO)currentEntry;
					if (type.compareToIgnoreCase("term")==0)
						mainTable.setWidget(1, areaCounter++, new StarButtonPanel(dataVO, areaVO, true));
				} catch (ClassCastException cce1) {
					try {
						StarSourceVO sourceVO=(StarSourceVO)currentEntry;
						if (type.compareToIgnoreCase("term")==0)
							mainTable.setWidget(2, sourceCounter++, new StarButtonPanel(dataVO, sourceVO, true));
					} catch (ClassCastException cce2) {
						try {
							StarLanguageVO languageVO=(StarLanguageVO)currentEntry;
							if (type.compareToIgnoreCase("term")==0)
								mainTable.setWidget(3, languageCounter++, new StarButtonPanel(dataVO, languageVO, true));
						} catch (ClassCastException cce3) {
							try {
								StarCommentVO commentVO=(StarCommentVO)currentEntry;
								mainTable.setWidget(4, commentCounter++, new StarDescriptionPanel(commentVO));
							} catch (ClassCastException cce4) {
								try {
									StarUseCaseVO usecaseVO=(StarUseCaseVO)currentEntry;
									if (type.compareToIgnoreCase("term")==0)
										mainTable.setWidget(5, usecaseCounter++, new StarDescriptionPanel(usecaseVO));
								} catch (ClassCastException cce5) {
									
								}
							}
						}
					}
				}
			}
		}
	}
	
	public StarSuggestionVO getSuggestionVO() {
		return suggestionVO;
	}
	
	public String getSuggestionText() {
		String text="";
		if (suggestionBox.getText()!=null)
			text=suggestionBox.getText();
		return text;
	}
	
	public StarEntryVO getDataVO() {
		return dataVO;
	}
	
	public String getDataCaption() {
		return captionBox.getText();
	}
	
	public StarButtonPanel getButtonPanel() {
		return buttonPanel;
	}
}
